﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: Propietario			*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.General.Entity;
using System.Data;
using ESF.General;
using ESF.SSO.Core.Entities;
using System.Collections.Specialized;

#endregion


namespace ESF.SSO.Core.DataAccess
{
	public static class AdoPerson
	{
		#region Write
		
		public static int? Register (Person pPerson)
		{
			IDbCommand cmd;
			
			NameValueCollection dbClientMsg;
			
			
			dbClientMsg = new NameValueCollection ();
			dbClientMsg.Add ("U_Person_Email", string.Format ("El correo electrónico {0} esta ya registrado para otro usuario, cambielo", pPerson.Email));
			dbClientMsg.Add ("U_Person_Identification", "La identificación proporcionada ya esta registrada para otro usuario, revisela");
			
			
			if (pPerson.ID == null)
			{
				cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_InsertPerson");
				
				AdoESFSSO.W.AddOutParameter (cmd, "pID", DbType.Int32); 
			}
			else
			{
				cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_UpdatePerson");
				
				AdoESFSSO.W.AddParameter (cmd, "pID", pPerson.ID);
			}
			
			AdoESFSSO.W.AddParameter (cmd, "pFirstName", pPerson.FirstName);
			AdoESFSSO.W.AddParameter (cmd, "pSecondName", pPerson.SecondName);
			AdoESFSSO.W.AddParameter (cmd, "pSurName", pPerson.SurName);
			AdoESFSSO.W.AddParameter (cmd, "pLastName", pPerson.LastName);
			AdoESFSSO.W.AddParameter (cmd, "pBirthDate", pPerson.BirthDate);
			AdoESFSSO.W.AddIDParameter (cmd, "pGenderID", pPerson.Gender.ID);
			AdoESFSSO.W.AddIDParameter (cmd, "pIdentificationTypeID", pPerson.IdentificationType);
			AdoESFSSO.W.AddParameter (cmd, "pIdentificationNumber", pPerson.IdentificationNumber);
			AdoESFSSO.W.AddParameter (cmd, "pEmail", pPerson.Email);
			AdoESFSSO.W.AddIDParameter (cmd, "pEnterpriseID", pPerson.Enterprise);
			AdoESFSSO.W.AddParameter (cmd, "pProfession", pPerson.Profession);
			AdoESFSSO.W.AddParameter (cmd, "pPosition", pPerson.Position);
			
			if (!Validate.IsFilledID (pPerson.Enterprise))
			{
				AdoESFSSO.W.AddNameParameter (cmd, "pOtherEnterprise", pPerson.Enterprise);
			}
			
			AdoESFSSO.W.Execute (cmd, dbClientMsg);
			
			pPerson.ID = AdoESFSSO.W.GetParameterValue <int> (cmd, "pID");
			
			RegisterLocations (pPerson);
			
			return pPerson.ID;
		}
		
		
		public static void RegisterLocations (Person pPerson)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_DeletePersonLocations");
		
			AdoESFSSO.W.AddIDParameter (cmd, "pPersonID", pPerson.ID);
		
			AdoESFSSO.W.Execute (cmd);
			
		
			foreach (Location pl in pPerson.Locations)
			{
				pl.ID = AdoLocation.Register (AdoLocation.AdjustValues (pl));
				
				cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_InsertPersonLocation");
				
				AdoESFSSO.W.AddIDParameter (cmd, "pPersonID", pPerson.ID);
				AdoESFSSO.W.AddIDParameter (cmd, "pLocationID", pl.ID);
				
				AdoESFSSO.W.Execute (cmd);
			}
		}
		
		#endregion
		
		
		
	}
}
